package per.dg.digital_klotski.views;

import android.util.Log;

import java.util.Random;

public class DigitalKlotskiManager {

    private int[][] data;

    private int row = 3;
    private int col = 3;
    
    private int[][] creatAndDaluanshunxu(int row, int col){
//        根据传入的行和列创建二维数组，
//        并赋值（1~row*col-1，最后一位为0）
        int[][] ints = new int[col][row];
        for(int i = 0; i < col; i++){
            for(int j = 0; j < row; j++){
                if(i == col - 1 && j == row - 1){
                    ints[j][i] = 0;
                }else {
                    ints[j][i] = i * col + j + 1;
                }
            }
        }
//
//        0的位置 col_0 ： 第几列；row_0 ：第几行
        int col_0 = col - 1, row_0 = row - 1;
//        x 在横向移动的最大距离， y 在纵向移动的最大距离
        int x = 0, y = 0;
        int max_x = 0, max_y = 0;
        Random random = new Random();
//        随机次数
//        int count = (int) Math.pow(row, col);
        int count = (int) Math.pow(4, 4);
        int temp_random = -1;
        int jiange = count / 4;
        for(int i = 0; i < count; i++){
//            取随机数 0：向左移动 1：向上移动 2：向右移动 3 向下移动
            while (true){
                int j = random.nextInt(4);
                if(i > jiange){
                    if(max_x < col-1){
                        if(temp_random == 2){
                            temp_random = 0;
                        }
                        j = 0;
                    }else if(max_y < row-1){
                        if(temp_random == 3){
                            temp_random = 1;
                        }
                        j = 1;
                    }
                }

                if(isQualified(j, row, col, row_0, col_0) && isQualified2(j, temp_random)){
                    temp_random = j;
                    ints = changeData(j, row_0, col_0, ints);
                    int[] position = changePosition(j, row_0, col_0);
                    row_0 = position[0];
                    col_0 = position[1];
                    if(j == 0){
                        x++;
                    }else if(j == 1){
                        y++;
                    }else if(j == 2){
                        x--;
                    }else if(j == 3){
                        y--;
                    }
                    if(max_x < x){
                        max_x = x;
                    }
                    if(max_y < y){
                        max_y = y;
                    }
                    break;
                }
            }
        }

        return ints;
    }

    private boolean isQualified2(int random, int temp_random){
        if(temp_random == -1){
            return true;
        }
        if(random + 2 == temp_random || random - 2 == temp_random){
            return false;
        }
        return true;
    }

    private boolean isQualified(int random, int row, int col, int row_0, int col_0){
        if(random == 0){
            col_0 -= 1;
            if(col_0 >= 0){
                return true;
            }else {
                return false;
            }
        }else if(random == 1){
            row_0 -= 1;
            if(row_0 >= 0){
                return true;
            }else {
                return false;
            }
        }else if(random == 2){
            col_0 += 1;
            if(col_0 < col){
                return true;
            }else {
                return false;
            }
        }else if(random == 3){
            row_0 += 1;
            if(row_0 < row){
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    private int[][] changeData(int random, int row_0, int col_0, int[][] data){
        if(random == 0){
            int temp = data[col_0][row_0];
            data[col_0][row_0] = data[col_0-1][row_0];
            data[col_0-1][row_0] = temp;
        }else if(random == 1){
            int temp = data[col_0][row_0];
            data[col_0][row_0] = data[col_0][row_0-1];
            data[col_0][row_0-1] = temp;
        }else if(random == 2){
            int temp = data[col_0][row_0];
            data[col_0][row_0] = data[col_0+1][row_0];
            data[col_0+1][row_0] = temp;
        }else if(random == 3){
            int temp = data[col_0][row_0];
            data[col_0][row_0] = data[col_0][row_0+1];
            data[col_0][row_0+1] = temp;
        }
        return data;
    }
    
    private int[] changePosition(int random, int row_0, int col_0){
        int[] ints = new int[2];
        if(random == 0){
            col_0 -= 1;
        }else if(random == 1){
            row_0 -= 1;
        }else if(random == 2){
            col_0 += 1;
        }else if(random == 3){
            row_0 += 1;
        }
        ints[0] = row_0;
        ints[1] = col_0;
        return ints;
    }

    public boolean isSuccess(int row, int col){
        boolean isSuccess = true;
        for(int i = 0; i < col; i++){
            for(int j = 0; j < row; j++){
                if(i == col - 1 && j == row - 1){
                    if(data[j][i] != 0){
                        isSuccess = false;
                        return isSuccess;
                    }
                }else {
                    if(data[j][i] != i * col + j + 1){
                        isSuccess = false;
                        return isSuccess;
                    }
                }
            }
        }
        return isSuccess;
    }

    public int[][] getData() {
        data = creatAndDaluanshunxu(row, col);
        return data;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public void setCol(int col) {
        this.col = col;
    }
}
